package com.zzh.ideas.web.controller;

import com.zzh.ideas.starter.service.StarterService;
import com.zzh.ideas.web.component.Test;
import com.zzh.ideas.web.service.MailService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Enumeration;

/**
 * description: <br>
 * createTime: 2018/7/1915:17 <br>
 *
 * @author zzh
 */
@RestController
public class IndexController extends BaseController {


    @Resource
    StarterService starterService;

    @Resource
    Test test;

    @Resource
    MailService mailService;

    @RequestMapping(value = "index", method = RequestMethod.GET)
    public ModelAndView index() throws IOException {
        return new ModelAndView("index");
    }

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String test(HttpServletRequest request) throws IOException {

        StringBuilder sb = new StringBuilder();
        Enumeration<String> headerNames = request.getHeaderNames();
        sb.append("requestUrl:\t" + request.getRequestURL());
        sb.append("<br/>");
        while(headerNames.hasMoreElements()) {
            String s = headerNames.nextElement();
            sb.append(s + ":\t" + request.getHeader(s) + "<br/>");
        }
        sb.append("<br/><br/>");
        sb.append("getScheme: \t" + request.getScheme() + "<br/>");
        sb.append("getLocalPort: \t" + request.getLocalPort() + "<br/>");
        sb.append("getServerPort: \t" + request.getServerPort() + "<br/>");
        sb.append("getServerName: \t" + request.getServerName() + "<br/>");
        sb.append("getRemotePort: \t" + request.getRemotePort() + "<br/>");
        sb.append("getLocalAddr: \t" + request.getLocalAddr() + "<br/>");
        sb.append("getRemoteAddr: \t" + request.getRemoteAddr() + "<br/>");
        sb.append("<br/><br/>");
        sb.append(request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath());
        return  sb.toString();
//        return Arrays.toString(starterService.split("_"));
    }

    @ResponseBody
    @RequestMapping(value = "/test666", method = RequestMethod.GET)
    public String test666() throws IOException {
        mailService.send("271292401@qq.com", "test", "test mail");
        return "hello world";
    }
}
